﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;

namespace Moving03
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        DataClasses1DataContext context = new DataClasses1DataContext();
        public static Guid lay_IDUser()
        {
            Guid userID = (Guid)Membership.GetUser().ProviderUserKey;
            return userID;
        }
        public void GanThongTinNoiDi()
        {
            
            var query = from nd in context.aspnet_Users
                        where nd.UserId == lay_IDUser()
                        select nd;

            foreach (var i in query)
            {
                Label1.Text = i.TenCongTy;
                Label2.Text = i.HoTen;
                var query1 = from datnuoc in context.DanhSachQuocGias
                             where datnuoc.Id_MaQGia == i.QuocGia
                             select datnuoc;
                foreach (var j in query1)
                    Label3.Text = j.TenQuocGia;

                Label4.Text = i.DiaChi1;
                Label5.Text = i.ThanhPho;
                Label6.Text = i.MaBuuDien.ToString();
                Label7.Text = i.DienThoai;
                Label8.Text = i.Email;
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (User.Identity.IsAuthenticated == false)
                {
                    Response.Redirect("Login.aspx");
                }
                if (User.Identity.IsAuthenticated == true && User.IsInRole("Admin"))
                {
                    Response.Redirect("Administrator/ThongtinAdmin.aspx");
                }
                GanThongTinNoiDi();
                ThongTinVanChuyen ttvc = (ThongTinVanChuyen)Session["ttvc"];
                NoiDen noiDen = (NoiDen)Session["noiDen"];
                NoiDi noiDi = (NoiDi)Session["noiDi"];
                LoHang loHang = (LoHang)Session["loHang"];
                MatHang matHang = (MatHang)Session["matHang"];
                if (ttvc != null)
                {
                    Session["soGoiCu"] = loHang.So_goi;
                    // danh sach goi hang
                    ddlloaigoi.DataSource = context.PhuongThucGoiHangs;
                    ddlloaigoi.DataTextField = "LoaiPhat";
                    ddlloaigoi.DataValueField = "Id_GoiHang";
                    ddlloaigoi.DataBind();
                    // danh sach phương thức thanh toán
                    ddlphuongthucthanhtoan.DataSource = context.PhuongThucThanhToans;
                    ddlphuongthucthanhtoan.DataTextField = "TenThanhToan";
                    ddlphuongthucthanhtoan.DataValueField = "Id_ThanhToan";
                    ddlphuongthucthanhtoan.DataBind();
                    // danh sach các quốc gia
                    ddlQuocGia.DataSource = context.DanhSachQuocGias;
                    ddlQuocGia.DataTextField = "TenQuocGia";
                    ddlQuocGia.DataValueField = "Id_MaQGia";
                    ddlQuocGia.SelectedValue = "2";
                    ddlQuocGia.DataBind();


                    aspnet_User user = context.aspnet_Users.Single(i => i.UserId == ttvc.Id_User);
                    Label1.Text = user.TenCongTy;
                    Label2.Text = user.HoTen;
                    DanhSachQuocGia getQG = context.DanhSachQuocGias.Single(i => i.Id_MaQGia == noiDi.Id_Quocgia);
                    Label3.Text = getQG.TenQuocGia;
                    Label4.Text = user.DiaChi1;
                    Label5.Text = noiDen.Thanhpho;
                    Label6.Text = noiDen.Mabuudien.ToString();
                    Label7.Text = noiDen.Dienthoai.ToString();
                    Label8.Text = noiDen.Email.ToString();

                    txtCongTy.Text = ttvc.TenDoanhNghiep;
                    txtTen.Text = ttvc.TenLienLac;
                    // danh sach các quốc gia
                    ddlQuocGia.DataSource = context.DanhSachQuocGias;

                    ddlQuocGia.DataTextField = "TenQuocGia";
                    ddlQuocGia.DataValueField = "Id_MaQGia";
                    ddlQuocGia.SelectedValue = noiDen.Id_Quocgia.ToString();
                    ddlQuocGia.DataBind();
                    txtDiaChi1.Text = noiDen.Diachi1;
                    txtDiaChi2.Text = noiDen.Diachi2;
                    txtThanhPho.Text = noiDen.Thanhpho;
                    txtMaBuuDien.Text = noiDen.Mabuudien.ToString();
                    txtDienThoai.Text = noiDen.Dienthoai.ToString();
                    txtEmail.Text = noiDen.Email;
                    if (noiDen.Nharieng == 1)
                    {
                        cbNhaRieng.Checked = true;
                    }

                    txtTenLienLacSuCo.Text = ttvc.TenNeuSuCo;
                    txtDiaChiTrahangve.Text = ttvc.DiaChiGoiNeuSuCo;
                    txtTenLoHang.Text = loHang.Ten_lo_hang;

                    if (!IsPostBack)
                    {
                        for (int i = 1; i <= 20; i++)
                        {
                            if (i == loHang.So_goi)
                            {
                                ddlsogoi.Items.Add(i.ToString());
                                ddlsogoi.SelectedValue = i.ToString();
                            }
                            else
                            {
                                ddlsogoi.Items.Add(i.ToString());
                            }

                        }
                    }
                    if (loHang.Giong_nhau == 1)
                    {
                        ddlgiongnhau.SelectedValue = "1";
                    }
                    else
                    {
                        ddlgiongnhau.SelectedValue = "0";
                        txtTenMatHang.Enabled = false;
                        txtTrongLuong1Goi.Enabled = false;
                        RequiredFieldValidator6.Enabled = false;
                        RequiredFieldValidator9.Enabled = false;
                        //for(int i = 0; i < 
                    }
                    ddlgiongnhau.DataBind();
                    if (matHang != null)
                    {
                        txtTenMatHang.Text = matHang.TenMatHang.ToString();
                        txtTrongLuong1Goi.Text = matHang.TrongLuong.ToString();
                    }

                    txtTongKhoiLuong.Text = loHang.TongTrong_luong.ToString();
                    txtGiaTriUocLuong.Text = loHang.Gia_tri.ToString();

                    // danh sach goi hang
                    ddlloaigoi.DataSource = context.PhuongThucGoiHangs;
                    ddlloaigoi.DataTextField = "LoaiPhat";
                    ddlloaigoi.DataValueField = "Id_GoiHang";
                    ddlloaigoi.SelectedValue = ttvc.Id_PhuongThucGoiHang.ToString();
                    ddlloaigoi.DataBind();

                    txtBimat1.Text = ttvc.ThongTinBiMat1;
                    txtBiMat2.Text = ttvc.ThongTinBiMat2;
                    if (ttvc.NhanEmail == 1)
                    {
                        cbEmail.Checked = true;
                    }

                    if (ttvc.DungKiGoi == 1)
                    {
                        cbKiGoi.Checked = true;
                    }

                    if (ttvc.TacDongKhiHau == 1)
                    {
                        cbTacDong.Checked = true;
                    }

                    ddlphuongthucthanhtoan.DataSource = context.PhuongThucThanhToans;
                    ddlphuongthucthanhtoan.DataTextField = "TenThanhToan";
                    ddlphuongthucthanhtoan.DataValueField = "Id_ThanhToan";
                    ddlphuongthucthanhtoan.SelectedValue = ttvc.Id_PhuongThucThanhToan.ToString();
                    ddlphuongthucthanhtoan.DataBind();
                }
                else
                {
                    // danh sach các quốc gia
                    ddlQuocGia.DataSource = context.DanhSachQuocGias;

                    ddlQuocGia.DataTextField = "TenQuocGia";
                    ddlQuocGia.DataValueField = "Id_MaQGia";
                    ddlQuocGia.SelectedValue = "2";
                    ddlQuocGia.DataBind();
                    // danh sach goi hang
                    ddlloaigoi.DataSource = context.PhuongThucGoiHangs;
                    ddlloaigoi.DataTextField = "LoaiPhat";
                    ddlloaigoi.DataValueField = "Id_GoiHang";
                    ddlloaigoi.DataBind();
                    // danh sach phương thức thanh toán
                    ddlphuongthucthanhtoan.DataSource = context.PhuongThucThanhToans;
                    ddlphuongthucthanhtoan.DataTextField = "TenThanhToan";
                    ddlphuongthucthanhtoan.DataValueField = "Id_ThanhToan";
                    ddlphuongthucthanhtoan.DataBind();
                    if (!IsPostBack)
                    {
                        //ddlgiongnhau.Items.Add("Có");
                        //ddlgiongnhau.Items.Add("Không");

                        for (int i = 1; i <= 20; i++)
                        {
                            ddlsogoi.Items.Add(i.ToString());
                        }
                    }
                }
            }
        }
        public int insertNoiDi()
        {
            NoiDi ndi = new NoiDi();
            var query = from nd in context.aspnet_Users
                        where nd.UserId == lay_IDUser()
                        select nd;

            foreach (var i in query)
            {
                ndi.NguoiGoi = i.HoTen;
                ndi.Id_Quocgia = i.QuocGia;
                ndi.Diachi = i.DiaChi1;
                ndi.Thanhpho = i.ThanhPho;
                ndi.Mabuudien = i.MaBuuDien;
                ndi.Email = i.Email;
                ndi.Dienthoai = i.DienThoai;

            }
            Session["noiDi"] = ndi;
            //context.NoiDis.InsertOnSubmit(ndi);
            //context.SubmitChanges();
            return ndi.Id_Noidi;

        }
        protected void Button3_Click(object sender, EventArgs e)
        {
            int id_lohang = insertLoHang();
            int id_noiden = insertNoiDen();
            int id_noidi = insertNoiDi();
            int id_VC =  insertTTVC(id_lohang, id_noiden, id_noidi);
            //int id_ddcn = insertDiaDiemCapNhat(id_VC);
            //insertDuongDiChoLoHang(id_VC, id_ddcn);

        }
       

        private int insertTTVC(int id_lohang, int id_noiden, int id_noidi)
        {
            // insert thông tin vân chuyển
            ThongTinVanChuyen ttvc = new ThongTinVanChuyen();
            
            ttvc.Id_PhuongThucThanhToan = int.Parse(ddlphuongthucthanhtoan.SelectedValue.ToString());
            ttvc.Id_PhuongThucGoiHang = int.Parse(ddlloaigoi.SelectedValue.ToString());
            ttvc.ThongTinBiMat1 = txtBimat1.Text;
            ttvc.ThongTinBiMat2 = txtBiMat2.Text;
            ttvc.Id_LoHang = id_lohang;
            ttvc.TenDoanhNghiep = txtCongTy.Text;
            ttvc.TenLienLac = txtTen.Text;
            ttvc.IdNoiden = id_noiden;
            ttvc.IdNoiDi = id_noidi;
            ttvc.TenNeuSuCo = txtTenLienLacSuCo.Text;
            ttvc.DiaChiGoiNeuSuCo = txtDiaChiTrahangve.Text;
            ttvc.NgayDat = DateTime.Now;
            if (cbKiGoi.Checked == true)
                ttvc.DungKiGoi = 1;
            else
                ttvc.DungKiGoi = 0;

            if (cbEmail.Checked == true)
                ttvc.NhanEmail = 1;
            else
                ttvc.NhanEmail = 0;

            if (cbTacDong.Checked == true)
                ttvc.TacDongKhiHau = 1;
            else
                ttvc.TacDongKhiHau = 0;


            ttvc.DiaChiGoiNeuSuCo = txtDiaChiTrahangve.Text;
            ttvc.TenNeuSuCo = txtTenLienLacSuCo.Text;
            ttvc.TinhTrang = 1;
            ttvc.Id_User = lay_IDUser();
            Session["ttvc"] = ttvc;
            //context.ThongTinVanChuyens.InsertOnSubmit(ttvc);
            //context.SubmitChanges();

            int maVanChuyen = ttvc.Id_TTVC;
            if (ddlgiongnhau.SelectedValue.ToString() == "0")
            {
                MatHang matHang = (MatHang)Session["matHang0"];
                if(matHang != null && ddlgiongnhau.SelectedValue.ToString() != "Không")
                {
                    if (ddlsogoi.SelectedValue.ToString() == Session["soGoiCu"].ToString())
                    {
                        Response.Redirect("ThongTinLoHang.aspx?smh=" + ddlsogoi.SelectedValue.ToString() + "&idlh=" + id_lohang);
                    }
                    else
                    {
                        for (int i = 0; i < int.Parse(Session["soGoiCu"].ToString()); i++)
                        {
                            Session["matHang" + i] = null;
                        }
                        Response.Redirect("ThongTinLoHang.aspx?smh=" + ddlsogoi.SelectedValue.ToString() + "&idlh=" + id_lohang);
                    }
                }
                
                else
                {
                    Response.Redirect("ThongTinLoHang.aspx?smh=" + ddlsogoi.SelectedValue.ToString() + "&idlh=" + id_lohang);
                }
            }
            else 
            {
                Response.Redirect("HoanTatDatHang.aspx");
            }
            
            return maVanChuyen;
        }
        private int insertNoiDen()
        {
            // insert Noiden
            NoiDen nd = new NoiDen();
            nd.Email = txtEmail.Text;
            nd.Dienthoai = txtDienThoai.Text;
            nd.Diachi1 = txtDiaChi1.Text;
            nd.Diachi2 = txtDiaChi2.Text;
            int maquocgia = int.Parse(ddlQuocGia.SelectedValue.ToString());
            nd.Id_Quocgia = maquocgia;
      
            nd.Mabuudien = int.Parse(txtMaBuuDien.Text);
            nd.Nguoinhan = txtTen.Text;
            nd.Thanhpho = txtThanhPho.Text;
            if (cbNhaRieng.Checked)
                nd.Nharieng = 1;
            else
                nd.Nharieng = 0;

            nd.IdUser = lay_IDUser();
            Session["noiDen"] = nd;
            //context.NoiDens.InsertOnSubmit(nd);
            //context.SubmitChanges();
            int maNoiDen = nd.Id_NoiDen;
            return maNoiDen;
        }

        private int insertLoHang()
        {
            // insert bảng Lô hàng
            LoHang lh = new LoHang();
            if (ddlgiongnhau.SelectedValue.ToString() == "1")
            {
                lh.Giong_nhau = 1;
                
                MatHang matHang = new MatHang();
                matHang.TenMatHang = txtTenMatHang.Text;
                matHang.TrongLuong = double.Parse(txtTrongLuong1Goi.Text);
                matHang.TriGia = double.Parse(txtGiaTriUocLuong.Text);
                Session["matHangGiong"] = matHang;
            }
            else
                lh.Giong_nhau = 0;

            lh.Ten_lo_hang = txtTenLoHang.Text;
            lh.So_goi = int.Parse(ddlsogoi.SelectedValue.ToString());
            lh.Gia_tri = int.Parse(txtGiaTriUocLuong.Text);
            lh.TongTrong_luong = double.Parse(txtTongKhoiLuong.Text);

            Session["loHang"] = lh;
            //context.LoHangs.InsertOnSubmit(lh);
            //context.SubmitChanges();
            int id_lohang = lh.Id_LoHang;
            return id_lohang;
        }
    }
}